/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.model.zone.type;

import jmmos.gameserver.instancemanager.GuildHallManager;
import jmmos.gameserver.instancemanager.MapRegionManager.TeleportWhereType;
import jmmos.gameserver.model.actor.MMOCharacter;
import jmmos.gameserver.model.actor.instance.MMOPcInstance;
import jmmos.gameserver.model.entity.GuildHall;
import jmmos.gameserver.model.entity.guildhall.AuctionableHall;
import jmmos.gameserver.model.zone.MMOZoneRespawn;
import jmmos.gameserver.model.zone.ZoneId;
import jmmos.gameserver.network.serverpackets.AgitDecoInfo;

/**
 * A guild hall zone
 * @author durgus
 */
public class MMOGuildHallZone extends MMOZoneRespawn
{
	private int _guildHallId;
	
	public MMOGuildHallZone(int id)
	{
		super(id);
	}
	
	@Override
	public void setParameter(String name, String value)
	{
		if (name.equals("guildHallId"))
		{
			_guildHallId = Integer.parseInt(value);
			// Register self to the correct guild hall
			GuildHall hall = GuildHallManager.getInstance().getGuildHallById(_guildHallId);
			if (hall == null)
			{
				_log.warning("MMOGuildHallZone: Guild hall with id " + _guildHallId + " does not exist!");
			}
			else
			{
				hall.setZone(this);
			}
		}
		else
		{
			super.setParameter(name, value);
		}
	}
	
	@Override
	protected void onEnter(MMOCharacter character)
	{
		if (character.isPlayer())
		{
			// Set as in guild hall
			character.setInsideZone(ZoneId.GUILD_HALL, true);
			
			AuctionableHall guildHall = GuildHallManager.getInstance().getAuctionableHallById(_guildHallId);
			if (guildHall == null)
			{
				return;
			}
			
			// Send decoration packet
			AgitDecoInfo deco = new AgitDecoInfo(guildHall);
			character.sendPacket(deco);
			
		}
	}
	
	@Override
	protected void onExit(MMOCharacter character)
	{
		if (character.isPlayer())
		{
			character.setInsideZone(ZoneId.GUILD_HALL, false);
		}
	}
	
	@Override
	public void onDieInside(MMOCharacter character)
	{
	}
	
	@Override
	public void onReviveInside(MMOCharacter character)
	{
	}
	
	/**
	 * Removes all foreigners from the guild hall
	 * @param owningGuildId
	 */
	public void banishForeigners(int owningGuildId)
	{
		TeleportWhereType type = TeleportWhereType.GuildHall_banish;
		for (MMOPcInstance temp : getPlayersInside())
		{
			if ((temp.getGuildId() == owningGuildId) && (owningGuildId != 0))
			{
				continue;
			}
			
			temp.teleToLocation(type);
		}
	}
	
	/**
	 * @return the guildHallId
	 */
	public int getGuildHallId()
	{
		return _guildHallId;
	}
}
